/*-
 * Copyright (c) 2009-2010 David Forsythe.
 * See LICENSE file for license details.
 */

#ifndef __LIBPKG_PKG_INTERNAL_H__
#define __LIBPKG_PKG_INTERNAL_H__

#include <archive.h>
#include <limits.h>
#include <stdio.h>
#include <sys/queue.h>

#include "pkg_pkg.h"

typedef enum {
        /* Parseable meta files are parsed into a packages property list. */
        PARSEABLE,
        /* Script meta files are path-verified and added to a packages script
         * list. */
        SCRIPT
} meta_t;

struct meta_entry {
        meta_t type;
        int code; /* Only needs lower 16 bits. */
        const char *name;
};

struct _script {
        const char *name;
        char *pathname;
        TAILQ_ENTRY(_script) next;
};

struct parser_writer_vtable {
        /* Parsers. */
        int (*parse_FILE) (struct pkg *, FILE *, const char *);
        int (*parse_fd) (struct pkg *, int, size_t, const char *);
        int (*parse) (struct pkg *, char *, const char *);
        /* Writers. */
        int (*write_FILE) (struct pkg *, FILE *, const char *);
        int (*write_fd) (struct pkg *, int, const char *);
        int (*write) (struct pkg *, char **, const char *);
        /* Meta check. */
        int (*meta) (const char *);
};

struct install_vtable {
        int (*execute) (struct pkg *, const char *, const char *);
        int (*extract) (struct pkg *, struct archive *, struct archive_entry *);
        int (*remove) (struct pkg *, const char *, const char *);
};

/* Catalyst for database 'transactions'. */
struct pkg {
        unsigned long magic; /* Init. */

        struct pkg_logger *logger;
        char key[PATH_MAX];
        struct pkg_property *plist;
        struct parser_writer_vtable *vt;
        struct meta_entry *mt;
        TAILQ_HEAD(scripts_head, _script) scripts;
};

void _pkg_check_magic(struct pkg *, const char *);

#endif
